#!/usr/bin/env /Library/ringojs/bin/ringo

/**
 * @fileoverview
 * This small script is used to glob in the project_stats.json file and let
 * the user easily access the time-sorted stats at random.  This can be
 * used right at the console or within another script.
 */

var fs = require('fs'),
    clocsFile = fs.open('project_stats.json', {read: true}),
    startRead = Date.now(),
    clocsString = '[' + clocsFile.readLines().join(',') + ']',
    readTime = exports.readTime = Date.now() - startRead,
    startParse = Date.now(),
    stats = exports.stats = JSON.parse(clocsString),
    parseTime = exports.parseTime = Date.now() - startParse,

    len = stats.length,
    current = exports.current = len > 0 ? stats[len - 1] : null,
    previous = exports.previous = len > 1 ? stats[len - 2] : null;

clocsFile.close();

if (require.main === module) {
  current.date = new Date(current.date);
  print(JSON.stringify(current, null, '  '));
}
